package ca.cbc.giscommon.entity;

import ca.cbc.giscommon.util.StringUtil;
import com.vividsolutions.jts.geom.Geometry;

import javax.persistence.*;

import java.io.Serializable;
import java.sql.Timestamp;

/**
 * Story is the table store CBC feed
 *
 */
@Entity
@Table(name = "story")
public class Story extends BaseEntity implements Cloneable, Serializable {
    private static final long serialVersionUID = 1843894094245748540L;
	private long id;
	private String title;
	private String description;
	private String thumbnail;
	private String flag;
	private String storyUrl;
	private Timestamp publishDate;
	private Timestamp lastUpdatedDate;
//	private Geometry theGeom;
	private Category category;
	private Zone zone;
	private String videoClipIds;
	private String audioClipIds;
	private String authorName;
	private boolean cbcContent;
	
	public Story() {}
	
	public Story(long id, String title, String description, String thumbnail, String flag, String storyUrl,
			long publishDate, long lastUpdatedDate, Geometry theGeom, Category category, String videoClipIds,
			String audioClipIds, String authorName, boolean cbcContent) {
		this.setId(id);
		this.setTitle(StringUtil.instance.stripTags(title));
		this.setDescription(StringUtil.instance.stripTags(description));
		this.setThumbnail(StringUtil.instance.trimIfNotNull(thumbnail));
		this.setFlag(StringUtil.instance.trimIfNotNull(flag));
		this.setStoryUrl(StringUtil.instance.trimIfNotNull(storyUrl));
		this.setPublishDate(new Timestamp(publishDate));
		this.setLastUpdatedDate(new Timestamp(lastUpdatedDate));
		this.setTheGeom(theGeom);
		this.setCategory(category);
		this.setVideoClipIds(StringUtil.instance.trimIfNotNull(videoClipIds));
		this.setAudioClipIds(StringUtil.instance.trimIfNotNull(audioClipIds));
		this.setAuthorName(StringUtil.instance.trimIfNotNull(authorName));
		this.setCbcContent(cbcContent);
	}
	
	@Override
	public Story clone() throws CloneNotSupportedException {
		return (Story) super.clone();
	}
	
	public boolean equals(Object obj) {
		if(obj.getClass().equals(Story.class) && ((Story)obj).getId() == this.getId()) {
			return true;
		}
		return false;
	}
	
	@Column(name = "is_cbc_content")
	public boolean getCbcContent() {
		return cbcContent;
	}

	public void setCbcContent(boolean isCBCContent) {
		this.cbcContent = isCBCContent;
	}
	@Column(name = "audio_clip_ids")
	public String getAudioClipIds() {
		return audioClipIds;
	}

	@Column(name = "author_name")
	public String getAuthorName() {
		return authorName;
	}
	
	@ManyToOne
    @JoinColumn(name = "category_id")
	public Category getCategory() {
		return category;
	}
	
	public String getDescription() {
		return description;
	}

	public String getFlag() {
		return flag;
	}
	
	@Id
	public long getId() {
		return id;
	}
	
	@Column(name = "last_updated")
	public Timestamp getLastUpdatedDate() {
		return lastUpdatedDate;
	}

	@Column(name = "published_on")
	public Timestamp getPublishDate() {
		return publishDate;
	}

    @Column(name = "story_url")
	public String getStoryUrl() {
		return storyUrl;
	}

//	@Column(name = "the_geom")
//	@Type(type = "org.hibernatespatial.GeometryUserType")
//	public Geometry getTheGeom() {
//		return theGeom;
//	}

	public String getThumbnail() {
		return thumbnail;
	}

	public String getTitle() {
		return title;
	}

	@Column(name = "video_clip_ids")
	public String getVideoClipIds() {
		return videoClipIds;
	}

	@Transient
	public Zone getZone() {
		return zone;
	}

	public void setAudioClipIds(String audioClipIds) {
		this.audioClipIds = audioClipIds;
	}

	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public void setId(long id) {
		this.id = id;
	}
	
    public void setLastUpdatedDate(Timestamp lastUpdatedDate) {
		this.lastUpdatedDate = lastUpdatedDate;
	}
    
    public void setPublishDate(Timestamp publishDate) {
		this.publishDate = publishDate;
	}
    
    public void setStoryUrl(String storyUrl) {
		this.storyUrl = storyUrl;
	}
    
//    public void setTheGeom(Geometry theGeom) {
//		this.theGeom = theGeom;
//	}
    
    public void setThumbnail(String thumbnail) {
		this.thumbnail = thumbnail;
	}
    
    public void setTitle(String title) {
		this.title = title;
	}

    public void setVideoClipIds(String videoClipIds) {
		this.videoClipIds = videoClipIds;
	}

	public void setZone(Zone zone) {
		this.zone = zone;
	}

	public String toString() {
    	StringBuilder sf = new StringBuilder();
    	sf.append("Story {id: ").append(id)
    	  .append(", title:").append(title)
    	  .append(", flag:").append(flag)
    	  .append(", storyUrl:").append(storyUrl)
    	  .append(", publishDate:").append(publishDate)
    	  .append(", lastUpdatedDate:").append(lastUpdatedDate)
    	  .append(", videoClipIds:").append(videoClipIds)
    	  .append(", audioClipIds:").append(audioClipIds)
    	  .append(", authorName:").append(authorName)
    	  .append(", category:").append(category)
    	  .append(", theGome:").append(theGeom)
    	  .append(", zone:").append(zone)
    	  .append(", isCBCContent:").append(cbcContent)
    	  .append("}");
    	
    	return sf.toString();
    }
}
